﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.AlterManagement;
using Bricks.Business.Models.ProjectContract;
using Bricks.Site.Models;
using Bricks.Business.Data.Repositories.AlterManagement;
using Bricks.Business.Data.Repositories.ProjectContract;
using Bricks.Business.Service;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IAlterBackupService))]
    internal class AlterBackupService : IAlterBackupService
    {
        [Import]
        protected IAlterBackupRepository AlterBackupRepository { get; set; }
        [Import]
        protected IProjectInfoRepository ProjectInfoRepository { get; set; }
        [Import]
        protected IContractInfoRepository ContractInfoRepository { get; set; }
        
        public AlterBackupView GetAlterBackup(Guid alterID, string associatedNodeCode, string lastUpdateUserDepartment, string lastUpdateUser)
        {
            AlterBackupView alterBackupView = null;
            AlterBackup alterBackup = AlterBackupRepository.Entities.SingleOrDefault(a => a.AlterID == alterID && a.AssociatedNodeCode == associatedNodeCode && a.LastUpdateUserDepartment == lastUpdateUserDepartment && a.LastUpdateUser == lastUpdateUser);
            if (alterBackup!=null)
            {
                alterBackupView = new AlterBackupView();
                alterBackupView.ID = alterBackup.ID;
                alterBackupView.ProjectID = alterBackup.ProjectID;
                ProjectInfo projectInfo = ProjectInfoRepository.Entities.FirstOrDefault(p => p.ID == alterBackup.ProjectID);
                if (projectInfo != null)
                {
                    alterBackupView.ProjectName = projectInfo.ProjectName;
                }
                alterBackupView.ContractID = alterBackup.ContractID;
                ContractInfo contractInfo = ContractInfoRepository.Entities.FirstOrDefault(p => p.ID == alterBackup.ContractID);
                if (projectInfo != null)
                {
                    alterBackupView.ContractName = projectInfo.ProjectName;
                }
                alterBackupView.AlterCode = alterBackup.AlterCode;
                alterBackupView.AlterName = alterBackup.AlterName;
                alterBackupView.AlterPosition = alterBackup.AlterPosition;
                alterBackupView.AlterType = alterBackup.AlterType;
                alterBackupView.OriginalChartName = alterBackup.OriginalChartName;
                alterBackupView.AlterChartName = alterBackup.AlterChartName;
                alterBackupView.OriginalChartNo = alterBackup.OriginalChartNo;
                alterBackupView.AlterChartNo = alterBackup.AlterChartNo;
                alterBackupView.AlterReason = alterBackup.AlterReason;
                alterBackupView.AlterMoney = alterBackup.AlterMoney;
                alterBackupView.AlterDate = alterBackup.AlterDate;
                alterBackupView.RequestPerson = alterBackup.RequestPerson;
                alterBackupView.RequestDate = alterBackup.RequestDate;
                alterBackupView.ProcedureMonitor = alterBackup.ProcedureMonitor;
                alterBackupView.HandleState = alterBackup.HandleState;
                alterBackupView.LastUpdateUserDepartment = alterBackup.LastUpdateUserDepartment;
                alterBackupView.LastUpdateUser = alterBackup.LastUpdateUser;
                alterBackupView.LastUpdateTime = alterBackup.LastUpdateTime;
            }
            return alterBackupView;
        }

        public void SaveAlterBackup(AlterBackupView alterBackupView)
        {
            throw new NotImplementedException();
        }

        public void SaveAlter(AlterView alterView)
        {
            //Alter record = AlterRepository.Entities.FirstOrDefault(a => a.ID == alterView.ID);
            //if (record != null)
            //{
            //    UpdateAlter(record, alterView);
            //}
            //else
            //{
            //    InsertAlter(alterView);
            //}
        }

        private void InsertAlter(AlterView alterView)
        {
            Alter record = new Alter()
            {
                ID = alterView.ID,
                ProjectID = alterView.ProjectID,
                ContractID = alterView.ContractID,
                AlterCode = alterView.AlterCode,
                AlterName = alterView.AlterName,
                AlterPosition = alterView.AlterPosition,
                AlterType = alterView.AlterType,
                OriginalChartName = alterView.OriginalChartName,
                AlterChartName = alterView.AlterChartName,
                OriginalChartNo = alterView.OriginalChartNo,
                AlterChartNo = alterView.AlterChartNo,
                AlterReason = alterView.AlterReason,
                AlterMoney = alterView.AlterMoney,
                AlterDate = alterView.AlterDate,
                RequestPerson = alterView.RequestPerson,
                RequestDate = alterView.RequestDate,
                HandleState = alterView.HandleState,
                ProcedureMonitor = alterView.ProcedureMonitor,
                LastUpdateUserDepartment = alterView.LastUpdateUserDepartment,
                LastUpdateUser = alterView.LastUpdateUser,
                LastUpdateTime = alterView.LastUpdateTime
            };
            //AlterRepository.Insert(record);
        }

        private void UpdateAlter(Alter record, AlterView alterView)
        {
            record.ProjectID = alterView.ProjectID;
            record.ContractID = alterView.ContractID;
            record.AlterCode = alterView.AlterCode;
            record.AlterName = alterView.AlterName;
            record.AlterPosition = alterView.AlterPosition;
            record.AlterType = alterView.AlterType;
            record.OriginalChartName = alterView.OriginalChartName;
            record.AlterChartName = alterView.AlterChartName;
            record.OriginalChartNo = alterView.OriginalChartNo;
            record.AlterChartNo = alterView.AlterChartNo;
            record.AlterReason = alterView.AlterReason;
            record.AlterMoney = alterView.AlterMoney;
            record.AlterDate = alterView.AlterDate;
            record.RequestPerson = alterView.RequestPerson;
            record.RequestDate = alterView.RequestDate;
            record.ProcedureMonitor = alterView.ProcedureMonitor;
            record.HandleState = alterView.HandleState;
            record.LastUpdateUserDepartment = alterView.LastUpdateUserDepartment;
            record.LastUpdateUser = alterView.LastUpdateUser;
            record.LastUpdateTime = alterView.LastUpdateTime;
            //AlterRepository.Update(record);
        }

        public OperationResult Delete(string id)
        {
            try
            {
                PublicHelper.CheckArgument(id, "id");
                Guid guidID = Guid.Parse(id);
                
                return new OperationResult(OperationResultType.Success, "删除成功", null);
            }
            catch (Exception ex)
            {
                return new OperationResult(OperationResultType.Error, "删除失败");
            }
        }
    }
}